/*
    jQuery plugin for flexible layout
    Copyright (c) 2010 Keiji Hokamura (famteam, Johnnybroccolii)
    This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
 */

(function($) {
	
	var methods = {
	    center: function(settings, target) {
		    return target.each(function() {
			    var self = $(this).css('position', 'absolute');
			    if (self.data($.aligner.DATA) == null) {
			    	self.data($.aligner.DATA, {});
			    }
			    var data = self.data($.aligner.DATA);
			    data.resizeHandler = align;
			    
			    align();
			    $(window).resize(align);
			    function align() {
			    	if (settings.verticalAlign) {
			    		var baseHeight = $(window).height();
			    		baseHeight = settings.minimumHeight != null && baseHeight < settings.minimumHeight ? settings.minimumHeight : baseHeight;
			    		var height = settings.height != null ? settings.height : self.height();
					    self.css('top', (baseHeight - height) / 2 + settings.verticalMargin);
			    	}
			    	if (settings.horizontalAlign) {
			    		var baseWidth = $(window).width();
			    		baseWidth = settings.minimumWidth != null && baseWidth < settings.minimumWidth ? settings.minimumWidth : baseWidth;
			    		var width = settings.width != null ? settings.width : self.width();
					    self.css('left', (baseWidth - width) / 2 + settings.horizontalMargin);			    		
			    	}
				    if (!data.initialized) {
					    if (settings.initializeHandler != null) {
						    settings.initializeHandler(self);
					    }
					    data.initialized = true;
				    }
			    }
		    });
	    },
	    align: function(settings, target) {
		    return target.each(function() {
			    var self = $(this).css('position', 'absolute');
			    if (self.data($.aligner.DATA) == null) {
			    	self.data($.aligner.DATA, {});
			    }
			    var data = self.data($.aligner.DATA);
			    data.resizeHandler = align;
			    
			    function align() {
			    	if (self.css('right') != null && settings.maxLeft != null) {
			    		if ($(window).width() > settings.maxLeft && !data.adjustedWidth) {
				    		self.css('left', settings.maxLeft - self.innerWidth() - parseInt(self.css('right')));
				    		data.adjustedWidth = true;
			    		}
			    		if ($(window).width() < settings.maxLeft && data.adjustedWidth) {
			    			self.css('left', null);
				    		data.adjustedWidth = false;
			    		}
			    	}
			    	if (self.css('bottom') != null && (settings.maxTop != null || settings.minTop != null)) {
			    		if (settings.maxTop != null && $(window).height() > settings.maxTop && !data.adjustedHeight) {
				    		self.css('top', settings.maxTop - self.innerHeight() - parseInt(self.css('bottom')));
				    		data.adjustedHeight = true;
			    		}
			    		else if (settings.minTop != null && $(window).height() < settings.minTop && !data.adjustedHeight) {
				    		self.css('top', settings.minTop - self.innerHeight() - parseInt(self.css('bottom')));
				    		data.adjustedHeight = true;
			    		}
			    		if (($(window).height() < settings.maxTop || $(window).height() > settings.minTop) && data.adjustedHeight) {
			    			self.css('top', null);
			    			data.adjustedHeight = false;
			    		}
			    	}
				    if (!data.initialized) {
					    if (settings.initializeHandler != null) {
						    settings.initializeHandler(self);
					    }
					    data.initialized = true;
				    }
			    }
			    align();
			    $(window).resize(align);
		    });
	    },
	    cancel: function(target) {
	    	return target.each(function() {
	    		var self = $(this);
	    		var data = self.data($.aligner.DATA);
		    	if (data.resizeHandler != null) {
		    		$(window).unbind('resize', data.resizeHandler);
		    		self.data($.aligner.DATA, null);
		    	}
	    	});
	    },
	    realign: function(target) {
	    	return target.each(function() {
	    		var self = $(this);
	    		var data = self.data($.aligner.DATA);
		    	if (data.resizeHandler != null) {
		    		data.resizeHandler();
		    	}
	    	});
	    }
		
	};
	
	$.fn.aligner = function(settings) {
	    settings = $.extend({
	    	action: $.aligner.CENTER,
	        initializeHandler: null,
	        width: null,
	        height: null,
	    	// Center
	        verticalMargin: 0,
	        horizontalMargin: 0,
	        verticalAlign: true,
	        horizontalAlign: true,
	        minimumWidth: null,
	        minimumHeight: null,
	        // Align
	        maxLeft: null,
	        maxTop: null,
	        minTop: null
	    }, settings);

	    switch (settings.action) {
			case $.aligner.CENTER:
				return methods.center(settings, this);
			case $.aligner.ALIGN:
				return methods.align(settings, this);
			case $.aligner.CANCEL:
				return methods.cancel(this);
			case $.aligner.REALIGN:
				return methods.realign(this);
		}
	};
	
	$.aligner = {
		DATA: 'aligner-data',
		
		CANCEL: 'cancel',
		REALIGN: 'realign',
		CENTER: 'center',
		ALIGN: 'align'
	};
})(jQuery);